/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package newspaperSystem.Payment;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author Leo
 */
public class Payment extends javax.swing.JFrame implements ActionListener {

    /**
     * Creates new form Payment
     */
    private ButtonGroup btnGr = new ButtonGroup();

    public Payment() {
        initComponents();
        btnGr.add(jRadioButton1);
        btnGr.add(jRadioButton2);
        btnGr.add(jRadioButton3);

        jRadioButton1.addActionListener(this);
        jRadioButton2.addActionListener(this);
        jRadioButton3.addActionListener(this);

        jButton1.addActionListener(this);
        jButton2.addActionListener(this);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        table_payment = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Make Payment");

        jRadioButton1.setText("Check Paid");

        jRadioButton2.setText("Check Unpaid");

        jRadioButton3.setText("Display All");

        jLabel2.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
        jLabel2.setText("Amount:");

        jButton1.setText("Pay");

        jButton2.setText("Back");

        jTextField2.setEnabled(false);

        table_payment.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        table_payment.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                table_paymentMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(table_payment);

        jLabel4.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
        jLabel4.setText("Customer#");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(274, 274, 274)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(330, 330, 330)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton1)
                            .addComponent(jRadioButton2)
                            .addComponent(jRadioButton3))
                        .addGap(95, 95, 95)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addGap(33, 33, 33)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(19, 19, 19)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(144, 144, 144)
                        .addComponent(jRadioButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jRadioButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jRadioButton3)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(32, 32, 32))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(85, 85, 85)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(102, 102, 102))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void table_paymentMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_table_paymentMouseClicked
        // TODO add your handling code here:

        Locale loc = Locale.getDefault();
        ResourceBundle bundle = ResourceBundle.getBundle("database", loc);
        String url = bundle.getString("URL");
        String user = bundle.getString("username");
        String pw = bundle.getString("password");
        String driver = bundle.getString("Driver");
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement psmt = null;

        //PreparedStatement pstmnt;

        ResultSet resultSet = null;

        try {

            Class.forName(driver);

        } catch (ClassNotFoundException classNotFound) {

            System.out.println("OOps class not found:" + classNotFound);
        }


        try {

            conn = DriverManager.getConnection(url, user, pw);

            System.out.println("Connection successful");
            int row = table_payment.getSelectedRow();
            String tableClick = (table_payment.getModel().getValueAt(row, 0).toString());

            String sql4 = "Select * from Bill where cust_Id='" + tableClick + "'";
            psmt = conn.prepareStatement(sql4);
            resultSet = psmt.executeQuery();

            while (resultSet.next()) {

                String id = resultSet.getString("cust_id");
                jTextField2.setText(id);
            }

        } catch (SQLException excep) {
            System.out.println(excep);
        } finally {

            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }

            } catch (Exception exception) {
                System.out.println("Something went wrong along the way");
            }
        }
    }//GEN-LAST:event_table_paymentMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Payment.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Payment.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Payment.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Payment.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Payment().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTable table_payment;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {


        Locale loc = Locale.getDefault();
        ResourceBundle bundle = ResourceBundle.getBundle("database", loc);
        String url = bundle.getString("URL");
        String user = bundle.getString("username");
        String pw = bundle.getString("password");
        String driver = bundle.getString("Driver");
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement psmt = null;
        String sqlStmt = "Select username ,password from Hr ";
        //PreparedStatement pstmnt;

        ResultSet resultSet = null;

        try {

            Class.forName(driver);

        } catch (ClassNotFoundException classNotFound) {

            System.out.println("OOps class not found:" + classNotFound);
        }


        try {

            conn = DriverManager.getConnection(url, user, pw);

            System.out.println("Connection successful");
            stmt = conn.createStatement();



            if (e.getSource() == jRadioButton2) {
                String unpaidQuery = "Select* from Bill";
                resultSet = stmt.executeQuery(unpaidQuery);
                while (resultSet.next()) {
                    if (resultSet.getDouble(4) < 0) {
                        String sqlstmt = "Select * from Bill where AMOUNT = 15";
                        stmt = conn.prepareStatement(sqlstmt);
                        resultSet = psmt.executeQuery();
                        table_payment.setModel(DbUtils.resultSetToTableModel(resultSet));
                    }
                }
            }
            if (e.getSource() == jRadioButton1) {

                String paidQuery = "Select* from Bill";
                resultSet = stmt.executeQuery(paidQuery);

                while (resultSet.next()) {
                    if (resultSet.getDouble(4) >= 1) {
                        String sqlstmt = "Select * from Bill";
                        psmt = conn.prepareStatement(sqlstmt);
                        resultSet = psmt.executeQuery();

                        table_payment.setModel(DbUtils.resultSetToTableModel(resultSet));
                    }
                }
            }
            if (e.getSource() == jRadioButton3) {
                String sqlstmt = "Select * from Bill";
                psmt = conn.prepareStatement(sqlstmt);
                resultSet = psmt.executeQuery();

                table_payment.setModel(DbUtils.resultSetToTableModel(resultSet));
            }
            if (e.getSource() == jButton1) {
                String amnt = jTextField1.getText();
                String id = jTextField2.getText();
                String sql = "Update Bill set amount='" + (amnt) + "' where cust_id='" + (id) + "'";

                stmt.executeUpdate(sql);
                JOptionPane.showMessageDialog(null, "Thank you for paying on the account!!");
                // diplay updated
                String sqlstmt = "Select * from Bill";
                psmt = conn.prepareStatement(sqlstmt);
                resultSet = psmt.executeQuery();

                table_payment.setModel(DbUtils.resultSetToTableModel(resultSet));

            }
            if (e.getSource() == jButton2) {
                jTextField1.setText("");
                jTextField2.setText("");
                this.dispose();
            }

        } catch (SQLException excep) {
            System.out.println(excep);
        } finally {

            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }

            } catch (Exception exception) {
                System.out.println("Something went wrong along the way");
            }
        }
    }
}
